
package com.github.xuemingyu.springboot.acticitycloud.aop;

import com.bidanet.bdcms.core.bean.ApiResult;
import com.bidanet.bdcms.core.exception.CheckException;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.framework.cache.CacheManager;
import com.github.xuemingyu.springboot.acticitycloud.dao.DepartmentDao;
import com.github.xuemingyu.springboot.acticitycloud.driver.dingtalk.DingTalkService;
import com.github.xuemingyu.springboot.acticitycloud.entity.Department;
import com.github.xuemingyu.springboot.acticitycloud.service.DepartmentService;
import com.google.common.base.Strings;
import com.taobao.api.ApiException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;


/**
 * \* Created with IntelliJ IDEA.
 * \* User: xmy
 * \* Date: 2019/1/24
 * \* Time: 14:32
 * \
 */

@Aspect
@Component
public class ApiAspect {
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    DingTalkService dingTalkService;

    @Pointcut("execution(* com.github.xuemingyu.springboot.acticitycloud.controller.api.*.*(..))")
    public void excudeService() {
    }

    @Before("excudeService()")
    public void before() throws ApiException {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        HttpServletResponse response = sra.getResponse();

        String token = request.getHeader("userId");
        if (Strings.isNullOrEmpty(token)) {
            throw new CheckException("userId不存在");
        }
        CacheManager cacheManager = CacheManager.getInstance().putListener();
        Object o = cacheManager.get(token);
        if (Objects.isNull(o)) {
            throw new CheckException("登陆已过期");
        }
    /*    String parentDepts = dingTalkService.getParentDepts(token);
        OapiUserGetResponse user = dingTalkService.getUserInfoByUserId(token);
        Department firstByUserId = departmentDao.findFirstByUserId(token);
        if (Objects.isNull(firstByUserId)) {
            Department department = new Department();
            department.setDepartmentList(parentDepts);
            department.setUserId(token);
            department.setMobile(user.getMobile());
            departmentService.insert(department);
        } else {
            firstByUserId.setMobile(user.getMobile());
            firstByUserId.setDepartmentList(parentDepts);
            departmentDao.save(firstByUserId);
        }*/

    }

    @After("excudeService()")
    public void after() {
        //System.out.println("切面after执行了");
    }

    @AfterReturning("excudeService()")
    public void afterReturning() {
        //System.out.println("切面afterReturning执行了");
    }

    @AfterThrowing("excudeService()")
    public void afterThrowing() {
        //System.out.println("切面afterThrowing执行了");
    }

    @Around("excudeService()")
    public Object around(ProceedingJoinPoint thisJoinPoint) {
        Object obj = null;
        //System.err.println("切面around before执行了");
        try {
            obj = thisJoinPoint.proceed();
        } catch (Throwable e) {
            return ApiResult.error(e.getMessage());
        }
        //System.err.println("切面around after执行了");
        return obj;
    }

}

